﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace FinancialDB
{
    partial class addHomepage_Form : Form
    {
        XmlDocument xmldoc = new XmlDocument();
        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(@"FinancialDB.Users.userdata.xml");

        public addHomepage_Form()
        {
            InitializeComponent();
        }

        private void addHomepage_Form_Load(object sender, EventArgs e)
        {
            xmldoc.Load(stream);
            XmlNodeList columns = xmldoc.SelectNodes("/root/homepage/column");
            if (columns.Count != 0)
            {
                foreach (XmlNode co in columns)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = ((XmlElement)co).GetAttribute("name");
                    treeView1.Nodes[0].Nodes.Add(tn);
                }
            }
            treeView1.ExpandAll();
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 1;
            e.Node.SelectedImageIndex = 1;
        }

        private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
            e.Node.ImageIndex = 0;
            e.Node.SelectedImageIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode.Text.Equals("我的主页"))
            {
                MessageBox.Show("请选择一个子栏目！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(@"FinancialDB.Users.userdata.xml");
                xmldoc.Load(stream);
                XmlNode column = xmldoc.SelectSingleNode("/root/homepage/column[@name='" + this.treeView1.SelectedNode.Text + "']");

                if (column.SelectNodes("function[@name='" + this.textBox1.Text + "']").Count != 0)
                {
                    MessageBox.Show("该栏目已保存，请重新选择子栏目！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {                    
                    XmlElement func = xmldoc.CreateElement("function");
                    func.SetAttribute("name", this.textBox1.Text);
                    //func.SetAttribute("url", "");
                    column.AppendChild(func);
                    xmldoc.Save(stream);

                    DialogResult result = MessageBox.Show("该功能已添加到我的主页！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    if (result == DialogResult.OK)
                    {
                        this.Close();
                    }
                }                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form form = new newColumn_Form();
            form.WindowState = FormWindowState.Normal;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Show();
        }
    }
}
